<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_model extends MY_Model {

	var $table = 'category';
	
	//构造函数
	public function __construct() {
		//调用父类构造函数，必不可少
		parent::__construct();
	}

	/**
	 * 取得所有分类
	 * 
	 * @param array $config
	 * @param array $like
	 * @return array
	 */
	public function getCategoryAll($config = array(), $like = array()) {
		$config['totalCount'] = $this->get_search_num(NULL, $like);
		if($config['offset'] >= $config['totalCount']) {
			$config['offset'] = 0;
			$config['pageNum'] = 1;
 		}
 		
		$res = $this->search(NULL, $config, $like);
		$config['data'] = array();
		$ids = array();
		if(!empty($res)) {
			foreach ($res as $v) {
				$config['data'][$v['id']] = $v;
				$ids[] = $v['link_id'];
				$ids[] = $v['active_id'];
				$ids[] = $v['banner_id'];
			}
			$query = $this->db->select('id,source_id,img')->from('album')->where_in('id', $ids)->get();
			$rows = $query->result_array();
			foreach ($rows as $v) {
				switch($v['id']) {
					case $config['data'][$v['source_id']]['link_id']:
						$config['data'][$v['source_id']]['link_filename'] = $v['img'];
						break;
					case $config['data'][$v['source_id']]['active_id']:
						$config['data'][$v['source_id']]['active_filename'] = $v['img'];
						break;
					case $config['data'][$v['source_id']]['banner_id']:
						$config['data'][$v['source_id']]['banner_filename'] = $v['img'];
						break;
					default:break;
				}
			}
		}
		return $config;
	}

	/**
	 * 增加分类
	 * 
	 * @param array $data
	 * @return integer
	 */
	public function addCategory($data) {
		return $this->insert($data);
	}

	/**
	 * 删除分类
	 * 
	 * @param string $ids 多个用逗号隔开
	 * @return integer
	 */
	public function deleteCategory($ids) {
		$idsArr = explode(',', $ids);
		$this->db->trans_start();

		$query = $this->db->select('link_id,active_id,banner_id')
				->from($this->table)
				->where_in('id', $idsArr)
				->get();
		$rows = $query->result_array();
		if(!empty($rows)) {
			$data = array();
			foreach ($rows as $v) {
				$data[] = $v['link_id'];
				$data[] = $v['active_id'];
				$data[] = $v['banner_id'];
			}
			$query = $this->db->select('img')->from('album')->where_in('id', $data)->get();
			$rows = $query->result_array();
			foreach ($rows as $v) {
				if(file_exists($v['img'])) {
					unlink($v['img']);
				}
			}
			$this->db->where_in('id', $data);
			$this->db->delete('album');
		}
		$this->delete(array('id'=>$idsArr));
		
		$this->db->trans_complete();
		return $this->db->trans_status();
	}

	/**
	 * 更新分类信息
	 * 
	 * @param array $data
	 * @param integer $id
	 * @return integer
	 */
	public function updateCategory($data, $id) {
		return $this->update($data, array('id'=>$id));
	}

	/**
	 * 查找详细信息
	 * 
	 * @param integer $id
	 * @return array
	 */
	public function findCategory($id) {
		$query = $this->db->select('c.*, a.img as link_filename, aa.img as active_filename, aaa.img as banner_filename')
				->from($this->table . ' c')
				->join('album a', 'c.link_id = a.id', 'left')
				->join('album aa', 'c.active_id = aa.id', 'left')
				->join('album aaa', 'c.banner_id = aaa.id', 'left')
				->where(array('c.id'=>$id))
				->get();
		return $query->row_array();
	}
	
	public function categoryList() {
		$data = array();
		$result = $this->get_result(array('parent'=>0), array('select'=>'id,name'));
		if(!empty($result)) {
			foreach ($result as $v) {
				$data[$v['id']] = $v['name'];
			}
		}
		return $data;
	}
}
